/**   
* @Title: JXLSUtil.java 
* @Package com.kerrykidz.system.util 
* @Description: 
* Copyright: Copyright (c) 2014 
* Company:wuwh team by iss
* @author: zhuss   
* @date: 2014年9月12日 上午11:30:04 
* @version: V1.0
*
*/
package com.kerrykidz.system.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;  
import java.io.IOException;
import java.io.InputStream;  
import java.io.OutputStream;  
import java.util.ArrayList;  
import java.util.Date;
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  

import net.sf.jxls.transformer.XLSTransformer;  

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;  

import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.system.vo.ParSupperExportVo;

/**
 * 
 * @ClassName: JXLSUtil 
 * @description: JXLS报表生成工具
 * @author: zhuss 
 * @date: 2014年9月12日 上午11:30:08 
 * @version: V1.0
 *
 */
public class JXLSUtil {
	/** 
     * 导出excel 
     * @param templateFile - excel模版和生成的文件名称 
     * @param beans - 模版中填充的数据 
     */  
	public static String exportExcel(String templateFile,
			Map<String, Object> beans) {
		String suffix = PropertiesUtil.readValueFromReport("REPORT_SUFFIX");
		String templatePath =PropertiesUtil.readValueFromReport("REPORT_TEMPLATE_PATH")+templateFile+suffix;
		String destPath = PropertiesUtil.readValueFromReport("REPORT_DEST_PATH")+templateFile+suffix;
		FileUtil.createFile(PropertiesUtil.readValueFromReport("REPORT_DEST_PATH"));
		InputStream is = null;
		OutputStream os = null;
		try {
			//模板文件输入流
			is = JXLSUtil.class.getResourceAsStream(templatePath.trim());
			//生成模版输出流 
			os = new FileOutputStream(new File(destPath.trim()));
			XLSTransformer transformer = new XLSTransformer();
			Workbook workbook = transformer.transformXLS(is, beans);
			workbook.write(os);
			return destPath;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			try {
				os.flush();
				is.close();
				os.close();
			} catch (IOException e) {
			}
		}
	}
	
	/**导出 多个 sheet 的 excel 
	 * 
		 * @Description   
		 * String
		 * @author chenyu
		 * 2014年11月25日 下午1:44:51
	 */
	public static String exportMoreSheetExcel(String templateFile,
			 List<?> list,List<String> listSheetNames) {
		String suffix = PropertiesUtil.readValueFromReport("REPORT_SUFFIX");
		String templatePath =PropertiesUtil.readValueFromReport("REPORT_TEMPLATE_PATH")+templateFile+suffix;
		String destPath = PropertiesUtil.readValueFromReport("REPORT_DEST_PATH")+templateFile+suffix;
		FileUtil.createFile(PropertiesUtil.readValueFromReport("REPORT_DEST_PATH"));
		try {
			//模板文件输入流
			InputStream is = JXLSUtil.class.getResourceAsStream(templatePath.trim());
		 
			//======
	        //调用引擎生成excel报表  
	        XLSTransformer transformer = new XLSTransformer();  
		HSSFWorkbook workbook = (HSSFWorkbook) transformer
				.transformMultipleSheetsList(is, list, listSheetNames,templateFile, new HashMap<Object, Object>(), 0); 
	        System.out.println(workbook.getNumberOfSheets());
	        saveWorkbook(workbook, destPath.trim());
	        is.close(); 
			//======
			return destPath;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static void saveWorkbook(HSSFWorkbook  resultWorkbook, String fileName)throws Exception {
		OutputStream os = new BufferedOutputStream(new FileOutputStream(fileName));
		resultWorkbook.write(os);
		os.flush();
		os.close();
	}

	 public static void main(String[] args) throws Exception {  
	        String templateFile="jxls_template";  
	        //获取Excel模板文件  
	        String fileDir = JXLSUtil.class.getResource("").getFile();  
	        System.out.println(fileDir);
	        String filePath = fileDir + "parSupplyTemplate.xls";  
	        System.out.println("excel template file:" + filePath);  
	        FileInputStream is = new FileInputStream(filePath);  
	      
	       
	       
	        ParSupperExportVo p1= new ParSupperExportVo();
	        
	        p1.setOptTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
			p1.setOptUser("陈1");
			GblScmPrdEntity g11= new GblScmPrdEntity();
			g11.setPrdName("zhu食物1"); g11.setPurCnt(1);
			GblScmPrdEntity g12= new GblScmPrdEntity();
			g12.setPrdName("zhu食物2"); g12.setPurCnt(1);
			GblScmPrdEntity g13= new GblScmPrdEntity();
			g13.setPrdName("zhu食物3"); g13.setPurCnt(1);
			List<GblScmPrdEntity> gList =new ArrayList<GblScmPrdEntity>();
			gList.add(g11);gList.add(g12);gList.add(g13);
			p1.setParSupList(gList);p1.setType("食品");
			
			ParSupperExportVo p2= new ParSupperExportVo();
	        p2.setOptTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
			p2.setOptUser("陈2");p2.setParSupList(gList);p2.setType("装饰物");
			 List<ParSupperExportVo> list = new ArrayList<ParSupperExportVo>();
			 list.add(p1);
			 list.add(p2);
	    //=========================    
	        //sheet的名称  
	        List<String> listSheetNames = new ArrayList<String>();  
	        listSheetNames.add("食品类");  
	        listSheetNames.add("非食品类");  
	      
	        //调用引擎生成excel报表  
//	        XLSTransformer transformer = new XLSTransformer();  
////	        HSSFWorkbook wb=transformer.transformMultipleSheetsList(is, list,listSheetNames,"report",new HashMap<Object,Object>(),0);
//		HSSFWorkbook workbook = (HSSFWorkbook) transformer
//				.transformMultipleSheetsList(is, list, listSheetNames,
//						"report", new HashMap<Object, Object>(), 0); 
//	        System.out.println(workbook.getNumberOfSheets());
//	        saveWorkbook(workbook, "d:\\par.xls");
//	        is.close();
	        exportMoreSheetExcel("parSupplyTemplate",
	   			  list,  listSheetNames);
	        
	    }    
	
 
}
